<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2017-11-02 21:56:10
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_dianping extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_dianping';
        $this->_pk            = 'dp_id';
        $this->_pre_cache_key = 'jamesonreaddianping_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 是否发过书评
    public function hasshuping($uid, $book_id, $nums = 1)
    {
       return intval(DB::result_first("SELECT count(dp_id) FROM %t WHERE uid=%d AND book_id=%d", array($this->_table, $uid, $book_id))) >= intval($nums);
    }
    // 热门书评
    public function fetch_hot($book_id)
    {
        $cacheid = 'hotshuping-'.$book_id;
        if($cache = $this->fetch_cache($cacheid)){
            return $cache;
        }
        $data = DB::fetch_all("SELECT d.dp_id,d.book_id,d.text,d.uid,d.addtime,d.yinyongtext,d.zhichi,d.fandui,m.username AS author FROM %t AS d LEFT JOIN %t AS m ON d.uid=m.uid  WHERE d.book_id=%d ORDER BY d.zhichi DESC,d.addtime DESC LIMIT %d", array($this->_table, 'common_member', $book_id, 6));
        if(!$data){
            return array();
        }
        $data = $this->redspiao($data);
        if ($data) {
            $this->store_cache($cacheid,$data);
            return $data;
        }
        return false;
    }
    private function redspiao($data)
    {
        foreach ($data as $key => $value) {
            $data[$key]['dashang'] = DB::result_first('SELECT SUM(price) FROM %t WHERE uid=% AND book_id=%d', array('jamesonread_dashang', $value['uid'], $value['book_id']));
        }
        return $data;
    }
    // 获取最新书评
    public function fetch_new($book_id, $start = 0, $size = 10)
    {
        $data = DB::fetch_all("SELECT d.*,m.username AS author FROM %t AS d INNER JOIN %t AS m ON d.uid=m.uid WHERE d.book_id=%d ORDER BY d.addtime DESC LIMIT %d,%d", array($this->_table, 'common_member', $book_id, $start, $size));
        $data = $this->redspiao($data);
        if ($data) {
            $data[0]['pages'] = ceil($this->count_by_bid($book_id) / 10);
            return $data;
        }
        return null;
    }
    public function fetch_by_new($book_id, $start = 0, $size = 10)
    {
        $data = DB::fetch_all("SELECT d.*,m.username AS author FROM %t AS d INNER JOIN %t AS m ON d.uid=m.uid WHERE d.book_id=%d ORDER BY d.addtime DESC LIMIT %d,%d", array($this->_table, 'common_member', $book_id, $start, $size));
        return $data;
    }
    /*书评总数*/
    public function count_by_bid($book_id)
    {
        return (int) DB::result_first('SELECT count(dp_id) FROM %t WHERE book_id=%d', array($this->_table, $book_id));
    }
    // 表态
    public function addBiaotai($dp_id, $type)
    {
        return (int) DB::query('UPDATE ' . DB::table('jamesonread_dianping') . ' SET ' . DB::field($type, '1', '+') . ' WHERE ' . DB::field('dp_id', $dp_id, '='));
    }
    public function count_all()
    {
        return (int) DB::result_first('SELECT count(dp_id) FROM %t', array($this->_table));
    }
    public function fetch_all_shuping($start, $size)
    {
        $data = (array) DB::fetch_all('SELECT d.dp_id,d.book_id,d.text,d.zhichi,d.fandui,b.book_name FROM %t as d INNER JOIN %t as b ON d.book_id=b.book_id LIMIT %d,%d', array($this->_table, 'jamesonread_books', $start, $size));
        return $data;
    }
}